package com.succez.server.servlet;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ServletContext类
 */
public class ServletContext {

    /**
     * Servlet的全路径名，可以为ServletName，value为全路径名
     */
    private final Map<String, String> servletInfos;

    /**
     * Servlet的URL映射规则,key为url，value为servletName
     */
    private final Map<String, String> servletMappings;

    /**
     * 所有Servlet的实例
     */
    private ConcurrentHashMap<String, Servlet> servletObj;

    public ServletContext(Map<String, String> servletInfos, Map<String, String> servletMappings,
            ConcurrentHashMap<String, Servlet> servletObj) {
        this.servletInfos = servletInfos;
        this.servletMappings = servletMappings;
        this.servletObj = servletObj;
    }

    /**
     * 根据url获取servlet名字
     * 
     * @param url
     * @return
     */
    public String getServletName(String url) {
        return servletMappings.get(url);
    }

    /**
     * 根据servlet的别名获取servlet的全路径名
     */
    public String getServletClassName(String servletName) {
        return servletInfos.get(servletName);
    }

    /**
     * 根据servlet别名获取servlet实例
     * 
     * @param servletName
     * @return
     */
    public Servlet getServlet(String servletName) {
        return servletObj.get(servletName);
    }

    public void addServletObj(String servletName, Servlet servlet) {
        synchronized (ConnectionHandleTask.class) {
            servletObj.put(servletName, servlet);
        }
    }
}